package com.smsc.headend.connector.service.impl;


import cn.hutool.core.date.SystemClock;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smsc.headend.connector.mapper.AnonyDeviceMapper;
import com.smsc.headend.connector.service.AnonyDeviceService;
import com.smsc.headend.module.asset.entity.AnonyDevice;
import org.springframework.stereotype.Service;


@Service
public class AnonyDeviceServiceImpl extends ServiceImpl<AnonyDeviceMapper, AnonyDevice> implements AnonyDeviceService {

    @Override
    public void loginAnonyDevice(String deviceSn, Long deviceType, String ipAddress) {
        AnonyDevice existDevice = lambdaQuery().eq(AnonyDevice::getDeviceSn, deviceSn).one();
        if(existDevice != null) {
            existDevice.setComStatus(0L);
            existDevice.setTvLogin(SystemClock.now() / 1000);
            existDevice.setIpAddress(ipAddress);
            updateById(existDevice);
            return;
        }
        AnonyDevice anonyDevice = new AnonyDevice();
        anonyDevice.setComStatus(0L);
        anonyDevice.setDeviceSn(deviceSn);
        anonyDevice.setDeviceType(deviceType);
        anonyDevice.setTvLogin(SystemClock.now() / 1000);
        anonyDevice.setIpAddress(ipAddress);
        save(anonyDevice);
    }

    @Override
    public void offlineAnonyDevice(String deviceSn) {
        lambdaUpdate().eq(AnonyDevice::getDeviceSn, deviceSn).set(AnonyDevice::getTvOffline,
                SystemClock.now() / 1000).set(AnonyDevice::getComStatus, 1L).update();
    }
}
